package edu.kpi.pzks2.ui.system;

import edu.kpi.pzks2.graph.Edge;
import edu.kpi.pzks2.graph.Node;
import edu.kpi.pzks2.ui.GraphEditor;
import edu.kpi.pzks2.ui.MainFrame;
import edu.uci.ics.jung.algorithms.shortestpath.BFSDistanceLabeler;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.UndirectedSparseGraph;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;

@SuppressWarnings("serial")
public class SystemEditor extends GraphEditor {
    
    public Graph<Node, Edge> getNeededGraph() {
        return new UndirectedSparseGraph<Node, Edge>();
    }
    
    public SystemEditor() {
        super();
        
        visual.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Node>() {
            @Override
            public String transform(Node v) {
                return Integer.toString(v.getId());
            }
        });
        visual.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<Edge>() {
            @Override
            public String transform(Edge v) {
                return "";
            }
        });
    }

    public boolean isGraphValid() {
        BFSDistanceLabeler<Node,Edge> labeler = new BFSDistanceLabeler<Node, Edge>();
        labeler.labelDistances(layout.getGraph(), layout.getGraph().getVertices().iterator().next());
        
        if (!labeler.getUnvisitedVertices().isEmpty()) {
            MainFrame.showLogMessage("System graph isn't connected.");
        } else {
            MainFrame.showLogMessage("System graph is valid.");
        }
        
        return labeler.getUnvisitedVertices().isEmpty();
    }
    
    public Graph<Node, Edge> getSystemGraph() {
        return visual.getGraphLayout().getGraph();
    }
}
